package cn.javaxc.mvcwork.util.common;
import cn.javaxc.mvcwork.framework.actions.TActionContext;
import cn.javaxc.mvcwork.modules.login.UserBean;
import java.sql.Timestamp;
import java.text.*;
import java.util.*;




public class DateUtils {
	public final static String fs = "/";
  
  public static java.sql.Date parseDateString(String tmp) {
    java.sql.Date dateValue = null;
    try {
      java.util.Date tmpDate = DateFormat.getDateInstance(DateFormat.SHORT).parse(
          tmp);
      dateValue = new java.sql.Date(new java.util.Date().getTime());
      dateValue.setTime(tmpDate.getTime());
      return dateValue;
    } catch (Exception e) {
      try {
        return java.sql.Date.valueOf(tmp);
      } catch (Exception e2) {
      }
    }
    return null;
  }


  
  public static java.sql.Timestamp parseTimestampString(String tmp) {
    java.sql.Timestamp timestampValue = null;
    try {
      java.util.Date tmpDate = DateFormat.getDateTimeInstance(
          DateFormat.SHORT, DateFormat.LONG).parse(tmp);
      timestampValue = new java.sql.Timestamp(new java.util.Date().getTime());
      timestampValue.setTime(tmpDate.getTime());
      return timestampValue;
    } catch (Exception e) {
      try {
        return java.sql.Timestamp.valueOf(tmp);
      } catch (Exception e2) {
      }
    }
    return null;
  }


  
  public static java.sql.Timestamp parseTimestampString(String tmp, String format) {
    java.sql.Timestamp timestampValue = null;
    SimpleDateFormat df = null;
    df = new SimpleDateFormat(format);
    try {
      //java.util.Date tmpDate = DateFormat.getDateTimeInstance().parse(tmp);
      java.util.Date tmpDate = df.parse(tmp, new ParsePosition(0));
      timestampValue = new java.sql.Timestamp(new java.util.Date().getTime());
      timestampValue.setTime(tmpDate.getTime());
      return timestampValue;
    } catch (Exception e) {
      try {
        return java.sql.Timestamp.valueOf(tmp);
      } catch (Exception e2) {
      }
    }
    return null;
  }


  
  public static java.sql.Date parseDateString(String tmp, String format) {
    java.sql.Date dateValue = null;
    SimpleDateFormat df = null;
    df = new SimpleDateFormat(format);
    try {
      java.util.Date tmpDate = df.parse(tmp, new ParsePosition(0));
      dateValue = new java.sql.Date(new java.util.Date().getTime());
      dateValue.setTime(tmpDate.getTime());
      return dateValue;
    } catch (Exception e) {
      try {
        return java.sql.Date.valueOf(tmp);
      } catch (Exception e2) {
      }
    }
    return null;
  }


  
  public static String getUserToServerDateTimeString(TimeZone timeZone, int dateFormat, int timeFormat, String date) {
    return getUserToServerDateTimeString(
        timeZone, dateFormat, timeFormat, date, Locale.getDefault());
  }


  
  public static String getUserToServerDateTimeString(TimeZone timeZone, int dateFormat, int timeFormat, String date, Locale locale) {
    String convertedDate = null;
    try {
      DateFormat localeFormatter = DateFormat.getDateInstance(
          dateFormat, locale);
      if (timeZone != null) {
        localeFormatter.setTimeZone(timeZone);
      }
      DateFormat serverFormatter = DateFormat.getDateTimeInstance(
          dateFormat, timeFormat);
      //convertedDate = serverFormatter.format(localeFormatter.parse(date));
      convertedDate = serverFormatter.format(
          new java.util.Date(localeFormatter.parse(date).getTime()));
    } catch (Exception e) {
      if (date != null && !"".equals(date)) {
        System.err.println("EXCEPTION: DateUtils-> Timestamp " + e);
      }
    }
    return convertedDate;
  }


  
  public static Timestamp getUserToServerDateTime(TimeZone timeZone, int dateFormat, int timeFormat, String date, Locale locale) {
    try {
      DateFormat localeFormatter = DateFormat.getDateInstance(
          dateFormat, locale);
      if (timeZone != null) {
        localeFormatter.setTimeZone(timeZone);
      }
      localeFormatter.setLenient(false);
      return new Timestamp(localeFormatter.parse(date).getTime());
    } catch (Exception e) {
      if (date != null && !"".equals(date)) {
        System.err.println("EXCEPTION: DateUtils-> Timestamp " + e);
      }
    }
    return null;
  }


  
  public static java.sql.Timestamp getUserToServerDateTime(Calendar cal, TimeZone timeZone) {
    java.sql.Timestamp timestampValue = null;
    try {
      String date = getDateString(cal);
      DateFormat localFormatter = DateFormat.getDateInstance(DateFormat.SHORT);
      if (timeZone != null) {
        localFormatter.setTimeZone(timeZone);
      }
      timestampValue = new java.sql.Timestamp(
          localFormatter.parse(date).getTime());
    } catch (Exception e) {
      System.out.println(
          "DateUtils-> getUserToServerDateTime Exception" + e.toString());
    }
    return timestampValue;
  }


  
  public static String getServerToUserDateString(TimeZone timeZone, int dateFormat, java.sql.Timestamp date) {
    SimpleDateFormat formatter = null;
    try {
      // Used by the Calendar for storing events in an array
      formatter = (SimpleDateFormat) SimpleDateFormat.getDateInstance(
          dateFormat);
      formatter.applyPattern("M/d/yyyy");
      formatter.setTimeZone(timeZone);
    } catch (Exception e) {
      System.err.println("EXCEPTION: DateUtils -> Timestamp " + date);
    }
    return formatter.format((java.util.Date) date);
  }


  
  public static String getServerToUserDateTimeString(TimeZone timeZone, int dateFormat, int timeFormat, java.sql.Timestamp date) {
    SimpleDateFormat formatter = null;
    try {
      //TODO: combine the Locale when User Locale that has been implemented
      formatter = (SimpleDateFormat) SimpleDateFormat.getDateTimeInstance(
          dateFormat, timeFormat);
      if (timeZone != null) {
        formatter.setTimeZone(timeZone);
      }
    } catch (Exception e) {
      System.err.println("EXCEPTION: DateUtils -> Timestamp " + date);
    }
    return formatter.format(date);
  }


  
  public static java.util.Date getDate(Calendar cal) {
    java.util.Date convertedDate = null;
    try {
      SimpleDateFormat formatter = (SimpleDateFormat) SimpleDateFormat.getDateInstance(
          DateFormat.SHORT);
      formatter.applyPattern("M/d/yyyy");
      String tmpDate = (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(
          Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR);
      convertedDate = formatter.parse(tmpDate);
    } catch (Exception e) {
      System.err.println("EXCEPTION: DateUtils -> Timestamp ");
    }
    return convertedDate;
  }


  
  public static String getDateString(Calendar cal) {
    return (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(
        Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR);
  }


  
  public static String getDatePath(java.util.Date fileDate) {
    return getDatePath(new java.sql.Timestamp(fileDate.getTime()));
  }


  
  public static String getDatePath(java.sql.Timestamp fileDate) {
    if (fileDate == null) {
      return "0000" + fs + "0000" + fs;
    }
    SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy");
    String datePathToUse1 = formatter1.format(fileDate);
    SimpleDateFormat formatter2 = new SimpleDateFormat("MMdd");
    String datePathToUse2 = formatter2.format(fileDate);
    return datePathToUse1 + fs + datePathToUse2 + fs;
  }


  
  public static String getFilename() {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    return formatter.format(new java.util.Date());
  }


  
  public static String getFilename(java.sql.Timestamp fileDate) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
    return formatter.format(fileDate);
  }


  
  public static Timestamp roundUpToNextFive(long millis) {
    Calendar cal = Calendar.getInstance();
    while (cal.get(Calendar.MINUTE) % 5 != 0) {
      cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + 1);
    }
    return new Timestamp(cal.getTimeInMillis());
  }


  
  static int getDaysBetween(java.util.Calendar d1, java.util.Calendar d2) {
    if (d1.after(d2)) {
      // swap dates so that d1 is start and d2 is end
      java.util.Calendar swap = d1;
      d1 = d2;
      d2 = swap;
    }
    int days = d2.get(java.util.Calendar.DAY_OF_YEAR) -
        d1.get(java.util.Calendar.DAY_OF_YEAR);
    int y2 = d2.get(java.util.Calendar.YEAR);
    if (d1.get(java.util.Calendar.YEAR) != y2) {
      d1 = (java.util.Calendar) d1.clone();
      do {
        days += d1.getActualMaximum(java.util.Calendar.DAY_OF_YEAR);
        d1.add(java.util.Calendar.YEAR, 1);
      } while (d1.get(java.util.Calendar.YEAR) != y2);
    }
    return days;
  }



  public static String getDateAsString(Timestamp tmpTimestamp, Locale locale) {
    String dateAsString = "";
    try {
      SimpleDateFormat formatter = (SimpleDateFormat) SimpleDateFormat.getDateInstance(
          DateFormat.SHORT, locale);
      formatter.applyPattern(get4DigitYearDateFormat(formatter.toPattern()));
      dateAsString = formatter.format(tmpTimestamp);
    } catch (Exception e) {
    }
    return dateAsString;
  }


  
  public static String get4DigitYearDateFormat(String tmpDateFormat) {
    if (tmpDateFormat.indexOf("yy") > -1 && tmpDateFormat.indexOf("yyyy") == -1) {
      return tmpDateFormat.replace( "yy", "yyyy");
    } else {
      return tmpDateFormat;
    }
  }


  
  public static boolean isWeekend(Calendar cal) {
    return (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
        cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY);
  }


}
